package cn.peng.stu4teach.util.http;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.GsonUtils;

/**
 * 对象型响应数据
 */
public abstract class HttpGsonRe<T> implements HttpUtils.IHttpRe {
    private final Class<T> cls;

    public HttpGsonRe (Class<T> cls) {
        this.cls = cls;
    }

    @Override
    public void onHttpReSuccess (@NonNull String response) {
        T data = GsonUtils.fromJson(response, cls);
        if (null != data) {
            onSuccess(data);
        } else {
            onError("解析为结构体出错");
        }
    }

    @Override
    public void onHttpReError (String errorMessage) {
        onError(errorMessage);
    }

    protected abstract void onSuccess (@NonNull T pReData);

    protected abstract void onError (@NonNull String pErrMsg);
}
